package com.example.demo.command;

import ch.selise.exercise.pattern.notepad.command.CommandExecutor;
import ch.selise.exercise.pattern.notepad.command.GetCommandParameter;
import ch.selise.exercise.pattern.notepad.exception.EmptyStackException;
import ch.selise.exercise.pattern.notepad.exception.ExecuteException;
import ch.selise.exercise.pattern.notepad.exception.UnsupportedCommandException;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Table;

public class EditActionCommand extends ActionCommand {
		
	private static final long serialVersionUID = 1211873615038324692L;
	
	public EditActionCommand(String command, Table table) {
		super(command,table);
	}

	@Override
	protected void postClickAction(ClickEvent event) {
		
		if (event.getButton().getData() != null) {	
			
			Integer rowId = (Integer) event.getButton().getData();
			Item rowItem = getTableX().getItem(rowId);
			if (event.getButton().getCaption().equalsIgnoreCase("save")) {

				String text = rowItem.getItemProperty("Text").getValue()
						.toString();
				System.out.println("Row Id : " + rowId);
				System.out.println("Text : " + text);

				CommandExecutor commandExecutor = CommandExecutor.getInstance();;
				try {
					commandExecutor.executeCommand(new GetCommandParameter(
							getCommand(), text, rowId));
				} catch (ExecuteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (EmptyStackException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedCommandException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					alterRowMode(rowId, true);
				}

			}else{
				alterRowMode(rowId, false);
			}
		}
	}
	
	private void alterRowMode(Integer itemId, boolean isEnable){
		
		Item item = getTableX().getContainerDataSource().getItem(itemId);
		if (item != null) {
			for (Object propId : item.getItemPropertyIds()) {
				try {
					Property x = item.getItemProperty(propId);
					if (x.getValue() instanceof AbstractSelect
							|| x.getValue() instanceof AbstractTextField) {
						((Field) x.getValue()).setReadOnly(isEnable);
					}
					if (x.getValue() instanceof Button
							|| x.getValue() instanceof NativeButton) {
						Button xB = (Button) x.getValue();
						if (xB.getCaption().equalsIgnoreCase("edit") || xB.getCaption().equalsIgnoreCase("save")) {
							if (xB.getCaption().equalsIgnoreCase("save")) {
								xB.setCaption("Edit");
							} else {
								xB.setCaption("Save");
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}
